package aimis.util;

import jakarta.servlet.http.HttpServletResponse;
import org.apache.tomcat.util.http.fileupload.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

public class DownloadUtils {

    public static void download(File file, String name, HttpServletResponse response) throws IOException {
        response.setContentLengthLong(file.length());
        String fileName = new String(name.getBytes("UTF-8"), "ISO_8859_1");
        response.setHeader("Content-disposition", "attachment; filename=" + fileName);
        FileInputStream input = new FileInputStream(file);
        OutputStream output = response.getOutputStream();
        IOUtils.copy(input, output);
        input.close();
        output.close();
    }

    public static void preview(File file, String miniType, String name, HttpServletResponse response) throws IOException {
        response.setContentLengthLong(file.length());
        String fileName = new String(name.getBytes("UTF-8"), "ISO_8859_1");
        response.setHeader("Content-disposition", "inline; filename=\"" + fileName+"\"");
        response.setContentType(miniType);
        FileInputStream input = new FileInputStream(file);
        OutputStream output = response.getOutputStream();
        IOUtils.copy(input, output);
        input.close();
        output.close();
    }


}
